import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../routes/routes.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

import 'package:provider/provider.dart';
import 'package:flutter_application_cyber/model/cn_net_response.dart';
import 'package:flutter_application_cyber/model/cn_strategy_model.dart';
import 'package:flutter_application_cyber/model/cn_strategy_vmodel.dart';

import 'package:flutter_application_cyber/tools/cn_common_global.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
  // 初始化通知帮助类
  CnGlobal.init();
  runApp(MultiProvider(providers: [
    ChangeNotifierProvider(create: (context) => ChangeUserInfoModel()),
    ChangeNotifierProvider(create: (context) => CnStrategyViewModel.getInfo()),
  ], child: const MyApp()));
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
  @override
  void initState() {
    super.initState();
  }

  static final GlobalKey<NavigatorState> navigatorKey =
      GlobalKey<NavigatorState>();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      navigatorKey: navigatorKey,
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        bottomNavigationBarTheme: const BottomNavigationBarThemeData(
          backgroundColor: Colors.white,
          selectedItemColor: Colors.red,
          unselectedItemColor: Colors.black,
          selectedLabelStyle: TextStyle(
            fontSize: 12,
          ),
        ),
      ),
      initialRoute: '/CnWelcomePage',
      onGenerateRoute: onGenerateRoute,
      builder: EasyLoading.init(),

      // 讲zh设置为第一项,没有适配语言时，英语为首选项
      // supportedLocales: S.delegate.supportedLocales,
      // builder: nil,
    );
  }

  //监听程序进入前后台的状态改变的方法   0前台  1后台
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    if (state == AppLifecycleState.paused) {
      //后台
      // AnchorWallViewModel.getInfo().postUserModeSwitch(1);
      //print("应用状态--处于后台======");
    } else if (state == AppLifecycleState.resumed) {
      //前台
      // AnchorWallViewModel.getInfo().postUserModeSwitch(0);
      //print("应用状态--进入前台======");
    }
  }

  @override
  void dispose() {
    super.dispose();
    WidgetsBinding.instance.removeObserver(this);
  }
}
